﻿

using System;
using System.ServiceModel;
using WCFClient.StudentServiceWithhttp;
using WCFClient.StudentServiceWithTcp;
using WCFClient.TeacherServiceWithhttp;
using WCFClient.TeacherServiceWithTcp;


namespace WCFClient
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("开始请求：");
            using (StudentServiceWithhttp.StudentServiceClient httpStudent = new StudentServiceWithhttp.StudentServiceClient("httpStudentEndpoint"))
            {
                var student = httpStudent.GetStudent(100);
                Console.WriteLine("开始请求 -学生服务 http：" + httpStudent.Endpoint.Address + student.Id + student.FirstName + student.LastName + student.Age);
                var name = httpStudent.GetStudentName(100);
                Console.WriteLine("开始请求 -学生服务 http：" + httpStudent.Endpoint.Address + name);
            }
            using (StudentServiceWithTcp.StudentServiceClient tcpStudent = new StudentServiceWithTcp.StudentServiceClient("tcpStudentEndpoint"))
            {
                var student = tcpStudent.GetStudent(100);
                Console.WriteLine("开始请求 -学生服务 tcp：" + tcpStudent.Endpoint.Address + student.Id + student.FirstName + student.LastName + student.Age);
                var name = tcpStudent.GetStudentName(100);
                Console.WriteLine("开始请求 -学生服务 tcp：" + tcpStudent.Endpoint.Address + name);
            }
            using (TeacherServiceWithhttp.TeacherServiceClient httpTeacher = new TeacherServiceWithhttp.TeacherServiceClient("httpTeacherEndpoint"))
            {
                var teacher = new TeacherServiceWithhttp.Teacher() {  Id = 100};
                httpTeacher.GetTeacherNameWithId(ref teacher.AppId, ref teacher.SeeeionId, ref  teacher.Age, ref teacher.Id, ref  teacher.Name, ref  teacher.Teach);
                Console.WriteLine("开始请求 -教师服务 http：" + httpTeacher.Endpoint.Address + teacher.AppId + teacher.SeeeionId + teacher.Id + teacher.Name + teacher.Age + teacher.Teach);
                httpTeacher.GetTeacherWithId(ref teacher.AppId, ref teacher.SeeeionId, ref  teacher.Age, ref teacher.Id, ref  teacher.Name, ref  teacher.Teach);
                Console.WriteLine("开始请求 -教师服务 http：" + httpTeacher.Endpoint.Address + teacher.AppId + teacher.SeeeionId + teacher.Id + teacher.Name + teacher.Age + teacher.Teach);
            }
            using (TeacherServiceWithTcp.TeacherServiceClient tcpTeacher = new TeacherServiceWithTcp.TeacherServiceClient("netTeacherEndpoint"))
            {
                var teacher = new TeacherServiceWithhttp.Teacher() { Id = 100 };
                tcpTeacher.GetTeacherNameWithId(ref teacher.AppId, ref teacher.SeeeionId, ref  teacher.Age, ref teacher.Id, ref  teacher.Name, ref  teacher.Teach);
                Console.WriteLine("开始请求 -教师服务 tcp：" + tcpTeacher.Endpoint.Address + teacher.AppId + teacher.SeeeionId + teacher.Id + teacher.Name + teacher.Age + teacher.Teach);
                tcpTeacher.GetTeacherWithId(ref teacher.AppId, ref teacher.SeeeionId, ref  teacher.Age, ref teacher.Id, ref  teacher.Name, ref  teacher.Teach);
                Console.WriteLine("开始请求 -教师服务 tcp：" + tcpTeacher.Endpoint.Address + teacher.AppId + teacher.SeeeionId + teacher.Id + teacher.Name + teacher.Age + teacher.Teach);
                
            }
            InstanceContext instanceContext = new InstanceContext(new TestCallBack());
            using (DuplexChannelFactory<TestServiceWithTcp.ITestService> channelFactory = new DuplexChannelFactory<TestServiceWithTcp.ITestService>(instanceContext, "CallBackTcpEndpoint"))
            {
                TestServiceWithTcp.ITestService proxy = channelFactory.CreateChannel();
                using(proxy as IDisposable)
                {
                    Console.WriteLine("开始请求 -测试双工服务 netTcpBinding：" + channelFactory.Endpoint.Address);               
                    proxy.Add(1, 2); System.Threading.Thread.Sleep(100);
                }
                channelFactory.Close();
            }
            using (DuplexChannelFactory<TestServiceWithTcp.ITestService> channelFactory = new DuplexChannelFactory<TestServiceWithTcp.ITestService>(instanceContext, "CallBackHttpEndpoint"))
            {
                TestServiceWithTcp.ITestService proxy = channelFactory.CreateChannel();
                using (proxy as IDisposable)
                {
                    Console.WriteLine("开始请求 -测试双工服务 wsDualHttpBinding：" + channelFactory.Endpoint.Address);
                    proxy.Add(3, 4); System.Threading.Thread.Sleep(50);
                }
                channelFactory.Close();
            }
            Console.WriteLine("结束请求：");
            Console.Read();
        }
    }

    public class TestCallBack : WCFClient.TestServiceWithTcp.ITestServiceCallback
    {

        public void Write(double x, double y, double result)
        {
            System.Console.WriteLine("双工回调客服端：X=" + x + ",Y=" + y + ",result=" + result);
        }

    }

}
